package com.example.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HtmlUtil;
import com.example.entity.Account;
import com.example.entity.Category;
import com.example.entity.User;
import com.example.entity.Written;
import com.example.mapper.CategoryMapper;
import com.example.mapper.UserMapper;
import com.example.mapper.WrittenMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WrittenService {

    @Resource
    WrittenMapper writtenMapper;
    @Resource
    CategoryMapper categoryMapper;
    @Resource
    UserMapper userMapper;

    public void add(Written written) {
        Account currentUser = TokenUtils.getCurrentUser();
        written.setUserId(currentUser.getId());
        written.setStatus("待审核");
        written.setTime(DateUtil.now());
        writtenMapper.insert(written);
    }

    public void update(Written written) {
        writtenMapper.updateById(written);
    }

    public void deleteById(Integer id) {
        writtenMapper.deleteById(id);
    }

    public List<Written> selectAll(Written written) {
        List<Written> list = writtenMapper.selectAll(written);
        for (Written dbWritten :list){
            dbWritten.setDescription(HtmlUtil.cleanHtmlTag(dbWritten.getContent()));
        }
        return list;
    }

    /*public List<Written> selectByStatus(Written written) {
        return writtenMapper.selectByStatus(written);
    }*/

    public PageInfo<Written> selectPage(Integer pageNum, Integer pageSize, Written written) {
        Account currentUser = TokenUtils.getCurrentUser();
        if ("USER".equals(currentUser.getRole())){
            written.setUserId(currentUser.getId());
        }
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Written> list = writtenMapper.selectAll(written);
        //实现模块之间的关联
        //list中存储了发贴空间的原始数据
        /*for (Written dbWritten :list) {
            //先拿到categoryId/userId
            Integer categoryId = dbWritten.getCategoryId();
            Integer userId = dbWritten.getUserId();
            //通过刚才获取的id从对应的数据库里通过主键拿到相应的数据
            Category category = categoryMapper.selectById(categoryId);
            User user = userMapper.selectById(userId.toString());
            if (ObjectUtil.isNotEmpty(category)) {
                //把分类表中的title值赋给categoryTitle
                dbWritten.setCategoryTitle(category.getTitle());
            }
            if (ObjectUtil.isNotEmpty(user)) {
                //把用户表中的username值赋给userName
                dbWritten.setUserName(user.getName());
            }
        }*/
        return PageInfo.of(list);
    }

    public List<Written> selectByRole(Written written) {
        Account currentUser = TokenUtils.getCurrentUser();
        if ("USER".equals(currentUser.getRole())){
            written.setUserId(currentUser.getId());
        }
        List<Written> list = writtenMapper.selectAll(written);
        for (Written dbWritten :list){
            dbWritten.setDescription(HtmlUtil.cleanHtmlTag(dbWritten.getContent()));
        }
        return list;
    }

    public PageInfo<Written> selectByStatus(Integer pageNum, Integer pageSize, Written written) {
        // 强制设置状态筛选条件
        if(!"审核通过".equals(written.getStatus())) {
            written.setStatus("审核通过");
        }
        //开启分页查询
        PageHelper.startPage(pageNum, pageSize);
        List<Written> list = writtenMapper.selectByStatus(written);
        return PageInfo.of(list);
    }

    public List<Written> selectByStatus2(Written written) {
        // 强制设置状态筛选条件
        if(!"审核通过".equals(written.getStatus())) {
            written.setStatus("审核通过");
        }
        List<Written> list = writtenMapper.selectByStatus(written);
        for (Written dbWritten :list){
            dbWritten.setDescription(HtmlUtil.cleanHtmlTag(dbWritten.getContent()));
        }
        return list;
    }

    public Written selectById(Integer id) {
        Written dbWritten = writtenMapper.selectById(id);
        //先拿到categoryId/userId
        Integer categoryId = dbWritten.getCategoryId();
        Integer userId = dbWritten.getUserId();
        //通过刚才获取的id从对应的数据库里通过主键拿到相应的数据
        Category category = categoryMapper.selectById(categoryId);
        User user = userMapper.selectById(userId.toString());
        if (ObjectUtil.isNotEmpty(category)) {
            //把分类表中的title值赋给categoryTitle
            dbWritten.setCategoryTitle(category.getTitle());
        }
        if (ObjectUtil.isNotEmpty(user)) {
            //把用户表中的username值赋给userName
            dbWritten.setUserName(user.getName());
            dbWritten.setUserAvatar(user.getAvatar());
        }
        return dbWritten;
        /*return writtenMapper.selectById(id);*/
    }
}
